package api

import (
	"errors"
	http2 "github.com/bighuangbee/gokit/network/http"
	"github.com/bighuangbee/gokit/network/validation"
	"github.com/gin-gonic/gin"
	"net/http"
)

func ErrorDefault(c *gin.Context, err error) {
	Error(c, http.StatusBadRequest, err)
}

func Error(c *gin.Context, code int, err error) {
	if err == nil{
		err = errors.New("错误")
	}
	c.AbortWithStatusJSON(http.StatusOK, gin.H{
		"code" 	: code,
		"msg" :   err.Error(),
		"data"	: nil,
	})
}

func InvalidParams(c *gin.Context, err error) {
	c.AbortWithStatusJSON(http.StatusOK, gin.H{
		"code": http2.INVALID_PARAMS,
		"msg":  http2.StatusText[http2.INVALID_PARAMS] +", "+ validation.Trans(err),
		"data": nil,
	})
}

//没有权限
func UnAuthorized(c *gin.Context, msg string){
	c.AbortWithStatusJSON(http.StatusOK, gin.H{
		"code" 	: http.StatusUnauthorized,
		"msg" :     http2.StatusText[http.StatusUnauthorized] + ", " + msg,
		"data"	: nil,
	})
}

//未认证，非法访问
func UnPerminssion(c *gin.Context, msg string){
	c.AbortWithStatusJSON(http.StatusOK, gin.H{
		"code" 	: http.StatusMethodNotAllowed,
		"msg" :     http2.StatusText[http.StatusMethodNotAllowed] + ", " + msg,
		"data"	: nil,
	})
}

func Success(c *gin.Context, data interface{}) {
	c.AbortWithStatusJSON(http.StatusOK, gin.H{
		"code" 	: http.StatusOK,
		"msg" :     http2.StatusText[http.StatusOK],
		"data"	: data,
	})
}

